/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.charts;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.time.Year;
import org.jfree.data.xy.DefaultIntervalXYDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.joda.time.DateTime;
import sk.lieskove301.jianghongtiao.liaad.prisoner.gene.GeneticInfo;
import sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.AgentsTimelineUnit;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public class ScatterPlot extends AbstractChart {

    private String title;
    private SelectStat stat;

    public ScatterPlot(String title, SelectStat.FUNC_TYPE statF) {
        super(title);
        this.title = title;
        this.stat = SelectStat.getInstance(statF);
    }

    protected XYDataset createDataset(
            List<Map<String, AgentsTimelineUnit>> agents) {

        List<String> used = new ArrayList<String>();
        XYSeriesCollection dataset = new XYSeriesCollection();

        //iterate throw all years
        for (Iterator<Map<String, AgentsTimelineUnit>> it = agents.iterator(); it.
                hasNext();) {
            Map<String, AgentsTimelineUnit> map = it.next();
                double i=0;
            //iterate in one year throw all agents
            for (Iterator<String> it1 = map.keySet().iterator(); it1.hasNext();) {
                String geneticInfo = it1.next();
                //if agent was already used. In different years can be different agents
                if (!used.contains(geneticInfo.toString())) {
                    XYSeries s1 = new XYSeries(geneticInfo.toString());
                    //find the same agents in whole years and select agents with the same code
                    for (Iterator<Map<String, AgentsTimelineUnit>> it2 = agents.
                            iterator(); it2.hasNext();) {
                        Map<String, AgentsTimelineUnit> map2 = it2.next();
                        //select agents with the same gen. code
                        AgentsTimelineUnit atu = map2.get(geneticInfo);
                        if(atu != null){
//                            DateTime year = new DateTime(atu.getTime()+1, 1,
//                                    1, 0, 0, 0, 0);
//                            TimeSeriesDataItem dataItem = new TimeSeriesDataItem(
//                                new Year(year.toDate()), stat.stat(atu)+i);
                            s1.add(atu.getTime(), stat.stat(atu)+i);
                        
                        }
                    }
                    dataset.addSeries(s1);
                    used.add(geneticInfo.toString());
                }
                i += 0.02;
            }
        }
        return dataset;

    }

    protected JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createScatterPlot(
                title, // title
                "Year", // x-axis label
                "Agent's score", // y-axis label
                dataset, // data
                PlotOrientation.VERTICAL, //orientation
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
                );

        chart.setBackgroundPaint(Color.white);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setDrawSeriesLineAsPath(true);
        }

        NumberAxis3D axis = new NumberAxis3D("Year");
        plot.setDomainAxis(axis);
        return chart;

    }

    public static ScatterPlot showDiagram(String title,
            List<Map<String, AgentsTimelineUnit>> agents, SelectStat.FUNC_TYPE statF){
        ScatterPlot result = new ScatterPlot(title, statF);
        result.main(agents);
        return result;
    }

}
